package com.gmcc.tripleplay.control;

import android.content.Context;
import com.android.util.Base64;
import com.gmcc.tripleplay.app.Constants;
import com.gmcc.tripleplay.entity.*;
import com.sara.loader.*;

public class AlbumControl {

    private static AlbumControl instance;

    private Context mContext;

    private AlbumControl(Context context) {
        mContext = context;
    }

    public static void init(Context context) {
        instance = new AlbumControl(context);
    }

    public static AlbumControl getInstance() {
        return instance;
    }

    public void getRecommendAlbumList(
            final ControlListener<AlbumListResponse> listener) {
        Request request = new Request();
        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.BASE_URL + "albumList/"
                    + Constants.API_KEY + "/" + Constants.CP_ID + "/10001/2/20/0");
        }

        JsonResponseParser<AlbumList> responseParser = new JsonResponseParser<AlbumList>(
                AlbumList.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000000".equals(response.getCode())) {
                    listener.onResponseSuccess(new AlbumListResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }

    public void getHeadlineAlbumList(
            final ControlListener<AlbumListResponse> listener) {
        Request request = new Request();
        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.BASE_URL + "albumList/"
                    + Constants.API_KEY + "/" + Constants.CP_ID + "/10001/1/20/0");
        }

        JsonResponseParser<AlbumList> responseParser = new JsonResponseParser<AlbumList>(
                AlbumList.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000000".equals(response.getCode())) {
                    listener.onResponseSuccess(new AlbumListResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }

    public void getRelativeAlbumList(String id,
            final ControlListener<AlbumListResponse> listener) {
        Request request = new Request();
        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL
                    + Constants.API_KEY + "/" + Constants.CP_ID + "/" + id);
        }

        JsonResponseParser<AlbumList> responseParser = new JsonResponseParser<AlbumList>(
                AlbumList.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000000".equals(response.getCode())) {
                    listener.onResponseSuccess(new AlbumListResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }

    public void getUserLoginInfo(
            final ControlListener<UserLoginResponse> listener) {
        Request request = new Request();
        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.BASE_URL + "userLogin_/"
                    + Constants.API_KEY + "/2/supermancode//1/");
        }

        JsonResponseParser<UserLogin> responseParser = new JsonResponseParser<UserLogin>(
                UserLogin.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000000".equals(response.getCode())) {
                    listener.onResponseSuccess(new UserLoginResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }

    public void getMPlayInfo(Album album,  String userToken,
            final ControlListener<MPlayResponse> listener) {
        Request request = new Request();
        String base64 = Base64.encodeToString(( Constants.API_KEY + "/" + Constants.CP_ID + "/" + album.vid + "/" + album.tvId + "/" + album.albumId).getBytes(), Base64.NO_WRAP);
        request.addRequestHeader("Itv-userToken", userToken);

        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.BASE_URL + "mplay/" + base64);
        }


        JsonResponseParser<MPlayInfo> responseParser = new JsonResponseParser<MPlayInfo>(
                MPlayInfo.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000002".equals(response.getCode())) {
                    listener.onResponseSuccess(new MPlayResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }

    public void getSeries(Album album,
                             final ControlListener<AlbumSeriesResponse> listener) {
        Request request = new Request();

        if (Constants.IS_INTERFACE_TEST) {
            request.setRequestUrl(Constants.GET_RECOMMEND_ALBUM_URL_TEST);
        } else {
            request.setRequestUrl(Constants.BASE_URL + "albumVideo/" + album.albumId + "/" + Constants.API_KEY + "/" + Constants.CP_ID);
        }

        JsonResponseParser<AlbumSeries> responseParser = new JsonResponseParser<AlbumSeries>(
                AlbumSeries.class, new ResponseListener() {
            @Override
            public void onResponse(Response response) {
                if ("N000000".equals(response.getCode())) {
                    listener.onResponseSuccess(new AlbumSeriesResponse(
                            response));
                } else {
                    listener.onResponseFail(response);
                }
            }
        });

        Sender sender = new Sender(mContext, request, responseParser);
        sender.send();
    }
}
